import React, { PureComponent } from 'react'

import { connect } from 'react-redux'

import { addBannersAction, addRecommendAction } from '../store/actionCreators'

import { getMultidata } from 'api/list'

class ReduxAxios extends PureComponent {
  // 请求的数据在AboutConnectReactRedux中展示
  async componentDidMount(){
    try {
      const response = await getMultidata()
      if (response.code === 200) {
        const data = response.rows
        this.props.setBanners(data)
        this.props.setRecommend(data)
      }
    } catch (error) {
      console.error(error);
      
    }
  }
  render() {
    return (
      <div>
        <h2>redux中异步操作</h2>

      </div>
    )
  }
}

const mapStateToProps = (state) => ({
  banners: state.banners,
  recommend: state.recommend
})

const mapDispatchToProps = (dispatch) => ({
  setBanners: (banners) => dispatch(addBannersAction(banners)),
  setRecommend: (recommend) => dispatch(addRecommendAction(recommend))
})

export default connect(mapStateToProps, mapDispatchToProps)(ReduxAxios)
